package net.esj.basic.core.view.tools.searcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import net.esj.basic.utils.CollectionUtils;

import org.apache.commons.lang.StringUtils;

public class SearcherImpl implements Searcher {

	private List<SearcherColumn> columns  = Collections.EMPTY_LIST;
	
	private List<SearcherColumn> orders = Collections.EMPTY_LIST;
	
	private String key;
	
	public SearcherImpl(String key) {
		super();
		this.key = key;
	}

	public SearcherImpl() {
		super();
	}

	@Override
	public void clearAll() {
		columns.clear();
	}

	@Override
	public void clearById(String id) {
		if(id==null){
			return;
		}
		int i =0;
		for(SearcherColumn col: columns){
			if(id.equalsIgnoreCase(col.getId())){
				columns.remove(i);
				return;
			}
			i++;
		}
	}

	@Override
	public void clearByKey(String key) {
		if(key ==null)
			return;
		List<SearcherColumn> newColumns = new ArrayList<SearcherColumn>();
		for(SearcherColumn col: columns){
			if(!key.equals(col.getKey())){
				newColumns.add(col);
			}
		}
		columns = newColumns;
	}

	@Override
	public Collection<SearcherColumn> getColumns() {
		return columns;
	}

	@Override
	public Collection<SearcherColumn> getOrders() {
		return orders;
	}
	
	@Override
	public Collection<SearcherColumn> getColumnsByKey(String key) {
		List<SearcherColumn> reval = new ArrayList<SearcherColumn>();
		for(SearcherColumn col: columns){
			if(key.equals(col.getKey())){
				reval.add(col);
			}
		}
		return reval;
	}

	@Override
	public void newColumn(String key, String view,String type, Object value) {
		SearcherColumn column = new SearcherColumn(key, view,type, value);
		if(columns == Collections.EMPTY_LIST){
			columns = new ArrayList<SearcherColumn>();
		}
		columns.add(column);
	}

	@Override
	public void newColumn(SearcherColumn column) {
		if(columns == Collections.EMPTY_LIST){
			columns = new ArrayList<SearcherColumn>();
		}
		columns.add(column);
	}

	@Override
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	@Override
	public void newOrderBy(String key, String view, Object value, int index) {
		SearcherColumn column = new SearcherColumn(key, view, value,index);
		if(orders == Collections.EMPTY_LIST){
			orders = new ArrayList<SearcherColumn>();
		}
		orders.add(column);
		sortColumn(orders);
	}

	@Override
	public void newOrderBy(String key, String view, Object value) {
		SearcherColumn column = new SearcherColumn(key, view, value);
		if(orders == Collections.EMPTY_LIST){
			orders = new ArrayList<SearcherColumn>();
		}
		boolean exists = false;
		for(SearcherColumn c: orders){
			if(StringUtils.equals(c.getKey(), column.getKey())){
				c.setValue(column.getValue());
				c.setOrder(column.getOrder());
				c.setView(column.getView());
				exists= true;
				break;
			}
		}
		if(!exists){
			orders.add(column);
		}
		sortColumn(orders);
	}

	@Override
	public void newOrderBy(SearcherColumn column, int index) {
		column.setOrder(index);
		if(orders == Collections.EMPTY_LIST){
			orders = new ArrayList<SearcherColumn>();
		}
		boolean exists = false;
		for(SearcherColumn c: orders){
			if(StringUtils.equals(c.getKey(), column.getKey())){
				c.setValue(column.getValue());
				c.setOrder(column.getOrder());
				c.setView(column.getView());
				exists= true;
				break;
			}
		}
		if(!exists){
			orders.add(column);
		}
		sortColumn(orders);
	}

	@Override
	public void newOrderBy(SearcherColumn column) {
		if(orders == Collections.EMPTY_LIST){
			orders = new ArrayList<SearcherColumn>();
		}
		boolean exists = false;
		for(SearcherColumn c: orders){
			if(StringUtils.equals(c.getKey(), column.getKey())){
				c.setValue(column.getValue());
				c.setOrder(column.getOrder());
				c.setView(column.getView());
				exists= true;
				break;
			}
		}
		if(!exists){
			orders.add(column);
		}
		sortColumn(orders);
		
	}

	private void sortColumn(List<SearcherColumn> columns){
		Collections.sort(columns, new Comparator<SearcherColumn>() {

			@Override
			public int compare(SearcherColumn o1, SearcherColumn o2) {
				if(o1.getOrder()>o2.getOrder())
					return 1;
				else if(o1.getOrder() == o2.getOrder()){
					return 0;
				}
				return -1;
			}
		});
	}

	@Override
	public void clearOrderAll() {
		orders.clear();
	}

	@Override
	public boolean isEmpty() {
		return CollectionUtils.isEmpty(columns);
	}

}
